package com.app.songbump;

import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.util.ArrayList;
import java.util.List;

import org.apache.http.NameValuePair;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.HttpClient;
import org.apache.http.client.ResponseHandler;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.impl.client.BasicResponseHandler;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.message.BasicNameValuePair;

import android.app.IntentService;
import android.content.Intent;
import android.util.Log;

public class SongBumpServer extends IntentService {

	public static final String ACTION = "action";
	
	public static final int BUMP = 0;
	public static final int GET_FRIENDS = 1;
	public static final int GET_SONGS = 2;
	public static final int ADD_USER = 3;
	public static final int NEW_SONG = 4;
	public static final int NOTHING_PLAYING = 5;
	
	public static final String BUMP_PATH = "/songbump/bump.php";
	public static final String GET_FRIENDS_PATH = "/songbump/getfriends.php";
	public static final String GET_SONGS_PATH = "/songbump/getsongs.php";
	public static final String ADD_USER_PATH = "/songbump/adduser.php";
	public static final String NEW_SONG_PATH = "/songbump/newsong.php";
	public static final String NOTHING_PLAYING_PATH = "/songbump/nothingplaying.php";
	
	public static final String BUMP_RESPONSE = "com.app.songbump.intent.action.BUMP";
	public static final String GET_FRIENDS_RESPONSE = "com.app.songbump.intent.action.GET_FRIENDS";
	public static final String GET_SONGS_RESPONSE = "com.app.songbump.intent.action.GET_SONGS";
	public static final String ADD_USER_RESPONSE = "com.app.songbump.intent.action.ADD_USER";
	public static final String NEW_SONG_RESPONSE = "com.app.songbump.intent.action.NEW_SONG";
	public static final String NOTHING_PLAYING_RESPONSE = "com.app.songbump.intent.action.NOTHING_PLAYING";
	
	HttpClient httpclient = null;
	
	public SongBumpServer() {
		super("SongBumpServer");
		httpclient = new DefaultHttpClient();
	}

	private String post(List<NameValuePair> qparams, String path){
		

		String responseBody = "";
		String baseUrl = "http://23.21.188.51"+path;
		
		UrlEncodedFormEntity entity = null;
		try {
			entity = new UrlEncodedFormEntity(qparams, "UTF-8");
		} catch (UnsupportedEncodingException e2) {
			e2.printStackTrace();
			return responseBody;
		}
		
		ResponseHandler<String> responseHandler = new BasicResponseHandler();
		
		try {
			HttpPost httppost = new HttpPost(baseUrl);
			httppost.setEntity(entity);
			Log.d("Adrian",httppost.getURI().toString());
			responseBody = httpclient.execute(httppost, responseHandler);
		} catch (ClientProtocolException e1) {
			e1.printStackTrace();
		} catch (IOException e1) {
			e1.printStackTrace();
		}
		
		return responseBody;
	}
	
	private String newSong(String id, String artist, String title, String ip){
		
		List<NameValuePair> qparams = new ArrayList<NameValuePair>();
		qparams.add(new BasicNameValuePair("id", id));
		qparams.add(new BasicNameValuePair("artist", artist));
		qparams.add(new BasicNameValuePair("title", title));
		qparams.add(new BasicNameValuePair("ip", ip));
		
		return post(qparams,NEW_SONG_PATH);
	}	
	
	private void nothingPlaying(String id){
		
		List<NameValuePair> qparams = new ArrayList<NameValuePair>();
		qparams.add(new BasicNameValuePair("id",  id));
		
		post(qparams,NOTHING_PLAYING_PATH);
	}	
	
	private String getFriends(String id){
		
		List<NameValuePair> qparams = new ArrayList<NameValuePair>();
		qparams.add(new BasicNameValuePair("id",  id));
		
		return post(qparams,GET_FRIENDS_PATH);
	}	

	private String addUser(String name, String ip){
		
		List<NameValuePair> qparams = new ArrayList<NameValuePair>();
		qparams.add(new BasicNameValuePair("name", name));
		qparams.add(new BasicNameValuePair("ip", ip));
		
		return post(qparams,ADD_USER_PATH);
	}	
	
	private void bump(String start, String end){
		
		List<NameValuePair> qparams = new ArrayList<NameValuePair>();
		qparams.add(new BasicNameValuePair("start", start));
		qparams.add(new BasicNameValuePair("end", end));
		
		post(qparams,BUMP_PATH);
	}	
	
	private String getSongs(String id){
		
		List<NameValuePair> qparams = new ArrayList<NameValuePair>();
		qparams.add(new BasicNameValuePair("id",  id));
		
		return post(qparams,GET_SONGS_PATH);
	}	
	
	private void handleNewSong(Intent intent){
		
		String id = intent.getStringExtra("id");
		String artist = intent.getStringExtra("artist");
		String title = intent.getStringExtra("title");
		String ip = intent.getStringExtra("ip");
		String blah = newSong(id,artist,title,ip);
		Log.v("debug",blah);
		
		Intent broadcastIntent = new Intent();
		broadcastIntent.addCategory(Intent.CATEGORY_DEFAULT);
		broadcastIntent.setAction(NEW_SONG_RESPONSE);		
		sendBroadcast(broadcastIntent);
		
	}
	
	private void handleNothingPlaying(Intent intent){
		
		String id = intent.getStringExtra("id");
		nothingPlaying(id);
		
		Intent broadcastIntent = new Intent();
		broadcastIntent.addCategory(Intent.CATEGORY_DEFAULT);
		broadcastIntent.setAction(NOTHING_PLAYING_RESPONSE);		
		sendBroadcast(broadcastIntent);
		
	}
	
	private void handleBump(Intent intent){
		
		String start = intent.getStringExtra("start");
		String end = intent.getStringExtra("end");
		
		bump(start,end);
		
		Intent broadcastIntent = new Intent();
		broadcastIntent.addCategory(Intent.CATEGORY_DEFAULT);
		broadcastIntent.setAction(BUMP_RESPONSE);		
		sendBroadcast(broadcastIntent);
		
	}
	
	private void handleAddUser(Intent intent){
		
		String name = intent.getStringExtra("name");
		String ip = intent.getStringExtra("ip");
		String response = addUser(name,ip);
		Log.d("Adrian","Response:"+response);
		
		Intent broadcastIntent = new Intent();
		broadcastIntent.addCategory(Intent.CATEGORY_DEFAULT);
		broadcastIntent.putExtra("response", response);
		broadcastIntent.setAction(ADD_USER_RESPONSE);		
		sendBroadcast(broadcastIntent);
		
	}
	
	private void handleGetFriends(Intent intent){
		
		String id = intent.getStringExtra("id");
		String response = getFriends(id);
		
		Intent broadcastIntent = new Intent();
		broadcastIntent.addCategory(Intent.CATEGORY_DEFAULT);
		broadcastIntent.putExtra("response", response);
		broadcastIntent.setAction(GET_FRIENDS_RESPONSE);		
		sendBroadcast(broadcastIntent);
		
	}
	
	private void handleGetSongs(Intent intent){
		
		String id = intent.getStringExtra("id");
		String response = getSongs(id);
		
		Intent broadcastIntent = new Intent();
		broadcastIntent.addCategory(Intent.CATEGORY_DEFAULT);
		broadcastIntent.putExtra("response", response);
		broadcastIntent.setAction(GET_SONGS_RESPONSE);		
		sendBroadcast(broadcastIntent);
		
	}
	
	@Override
	protected void onHandleIntent(Intent intent) {
		
		switch(intent.getIntExtra(ACTION,-1)){
		
			case BUMP: 
				handleBump(intent); break;
			case GET_FRIENDS:
				handleGetFriends(intent); break;
			case GET_SONGS:
				handleGetSongs(intent); break;
			case ADD_USER:
				handleAddUser(intent); break;
			case NEW_SONG:
				handleNewSong(intent); break;
			case NOTHING_PLAYING:
				handleNothingPlaying(intent); break;
			default:
				Log.v("debug","Unable to handle intent");
				return;
		}
		
	}
}
